/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.fields.input
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import net.babeuf.form.event.FieldEvent;
	import net.babeuf.form.fields.AbstractField;
	import net.babeuf.form.fields.IField;
	import net.babeuf.form.utils.TextFieldUtils;
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class InputField extends AbstractField implements IField
	{
		protected var _textField : TextField ; 
		protected var _defaultLabel : String = "" ; 	
		
		protected var _minSize : uint  = 0 ;
		protected var _maxSize : uint  = 255 ;
		
		
		public function InputField( textField : TextField, min : uint = 0, max : uint =  255) 
		{
			this._textField = textField ; 	
			_textField.type = TextFieldType.INPUT ; 
			_textField.addEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ; 
			_textField.addEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ; 
			_textField.selectable = true ;
			setSize(min, max) ; 
			setDefaultLabel("");  
			super() ;
		}
		
		override public function giveFocus() : void
		{
			if ( _textField &&  _textField.stage ) _textField.stage.focus = _textField ; 
		}
		
		public function setDefaultLabel( label : String ) : void
		{
			_defaultLabel = label ; 
			_textField.addEventListener(FocusEvent.FOCUS_IN, _handleFocusIn) ; 
			_textField.addEventListener(FocusEvent.FOCUS_OUT, _handleFocusOut) ; 
			_textField.addEventListener(Event.CHANGE, _handleInput ) ; 
		}
		
		override public function dispose():void 
		{
			super.dispose();
			if (  _textField ) 
			{
				_textField.removeEventListener(FocusEvent.FOCUS_IN, _handleFocusIn) ; 
				_textField.removeEventListener(FocusEvent.FOCUS_OUT, _handleFocusOut) ;
				_textField.removeEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ; 
				_textField.removeEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ; 
				_textField.removeEventListener(TextEvent.TEXT_INPUT, _handleInput ) ; 
			}
		}
		
		override public function setTabIndex(index:int):int 
		{
			_textField.tabIndex = index ; 
			return super.setTabIndex(index) ; 
		}
		
	
		
		public function setSize( min : uint = 0, max : uint = 255 ) : void
		{
			_minSize = min ; 
			_maxSize = max ; 
			_textField.maxChars = _maxSize ; 
		}
		
		public function setRestrict( restrict : String ) : void
		{
			_textField.restrict = restrict ; 
		}
		
		
		protected function _handleFocusIn(e:FocusEvent):void 
		{
			if ( _textField.text == _defaultLabel ) _textField.text = "" ; 
		}
		
		protected function _handleFocusOut(e:FocusEvent):void 
		{			
			if ( TextFieldUtils.trim(_textField.text) == "" ) _textField.text = _defaultLabel ; 
		}
		
		protected function _handleInput(e:Event):void 
		{
			_notifyChanged() ; 
		}
		
		override public function empty():void 
		{
			super.empty();
			_textField.text = _defaultLabel ; 
		}
		
		override public function validate():Boolean 
		{
			super.validate();
			var out : String = getValue() ; 
			if (  out.length == 0 && !optional )_error = _emptyError ; 
			if ( out.length > 0 && out.length < _minSize ) _error = _minError ;
			if ( out.length > _maxSize )  _error = _maxError ;		
			
			if ( _error != "" ) {_updateSensors(false) ; return false ; }
			
			return true ; 
		}
		
		public function get minSize() : uint
		{
			return _minSize ; 
		}
		
		public function get maxSize() : uint
		{
			return _maxSize ; 
		}
		
		override public function getValue():* 
		{
			return TextFieldUtils.trim(_textField.text ) == _defaultLabel ? "" : TextFieldUtils.trim(_textField.text ) ; 
		}
		
		override public function setValue(value:*):void 
		{
			_textField.text = TextFieldUtils.trim( String(value) );
		}
		
	}

}